import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

public class day_06_IO_Test_03 {
    public static void main(String[] args) throws IOException {
        // 对文件中的数据进行排序
        FileInputStream fileInputStream = new FileInputStream("sort.txt");
        StringBuilder stringBuilder = new StringBuilder();

        int b;
        while ((b = fileInputStream.read()) != -1) {
            stringBuilder.append((char) b);
        }

        fileInputStream.close();

        // 分隔字符串，保留数字部分
        String[] str = stringBuilder.toString().split("-");
        // 创建集合，存入转换类型后的数据
        ArrayList<Integer> arrayList = new ArrayList<>();

        // 遍历字符数组，转型，存入集合
        for (String s : str) {
            arrayList.add(Integer.parseInt(s));
        }

        // 排序
        Collections.sort(arrayList);

        // 写出文件
        FileWriter fileWriter = new FileWriter("sort.txt");

        // 遍历写出文件
        for (int i = 0; i < arrayList.size(); i++) {
            if (i == arrayList.size() - 1) {
                fileWriter.write(arrayList.get(i) + "");
            } else {
                fileWriter.write(arrayList.get(i) + "-");
            }
        }

        fileWriter.close();
    }
}
